package com.demo.modules.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.common.Res;
import com.demo.common.utils.UserUtils;
import com.demo.modules.dto.QueryDTO;
import com.demo.modules.entity.Register;
import com.demo.modules.service.RegisterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (register)核酸检测登记表控制层
 */
@RestController
@RequestMapping("register")
public class RegisterController {
    /**
     * 服务对象
     */
    @Resource
    private RegisterService registerService;

    /**
     * 查询核酸检测登记数据
     *
     * @return 分页数据
     */
    @GetMapping("list")
    public Res<List<Register>> selectList() {
        List<Register> list = registerService.selectPage(UserUtils.getUserId());
        return Res.success(list);
    }

    /**
     * 根据id值在表里是否存在判断是新增还是修改操作
     * @param entity
     * @return
     */
    @PostMapping("saveOrUpdate")
    public Res<String> saveOrUpdate(@RequestBody Register entity){
        if(entity.getUserId() == null){
            entity.setUserId(UserUtils.getUserId());
        }
        boolean result = registerService.saveOrUpdate(entity);
        if(result){
            return Res.successMsg("新增或修改成功");
        }
        return Res.errorMsg("新增或修改失败");
    }

    @DeleteMapping("delete")
    public Res<String> delete(Integer id){
        boolean result = registerService.removeById(id);
        if(result){
            return Res.successMsg("删除成功");
        }
        return Res.errorMsg("删除失败");
    }

}
